<?php
$g4_path = "../"; 
require_once ($g4_path.'common.php');

$title = "Super admin";
$menu = 5;
require_once ('./admin.head.php');
require_once ('./admin.lib.php');

$default = false;
if(!isset($_GET['y'], $_GET['m'], $_GET['w'])) {
    $default =true;
}


$year = isset($_GET['y']) && $_GET['y'] ? $_GET['y'] : date('Y');
$month = isset($_GET['m']) && $_GET['m'] ? $_GET['m'] : date('n');
$week = isset($_GET['w']) && $_GET['w'] ? $_GET['w'] : 'all';
$monthSelected = array($month=>'selected="selected"');
$yearSelected = array($year=>'selected="selected"');
$weekSelected = array($week=>'selected="selected"');

if($month == 'all') {
        if($week == 'all') {
                $firstDayofYear = mktime(0,0,0,1,1,$year);
                $lastDayofYear = mktime(0,0,0,12,31,$year);
                $starttime = date('Y-m-d', $firstDayofYear);
                $endtime = date('Y-m-d', $lastDayofYear);
        }
        else {
            alert('불가능한 연산입니다');
        }
}
if(($month != 'all' && $week == 'all') || $default) {
        $now = mktime(0,0,0,$month, 1, $year);
        $starttime = date('Y-m-01', $now);
        $endtime = date('Y-m-t', $now);
}
if($month != 'all' && $week != 'all' && !$default) {
    $firstDayofMonth = mktime(0,0,0,$month,1,$year);
    $lastDayofMonth = date('t', $firstDayofMonth);
    $janFirstDaysWeekDay = date('w',mktime(0,0,0,1,1,$year));
    if((2+6-date('w',$firstDayofMonth)) + 7 * ($week -2) > $lastDayofMonth) {
            alert('이 달은 '.$week.'주차가 없습니다');
    }

    $firstDaysWeekDay = date('w', $firstDayofMonth);
    $weeks = array();
    $weeks[1]  = array();
    
    $weeks[1][] = 1;
    $weeks[1][] = 1+(6-$firstDaysWeekDay);
    $weeks[2] = array($weeks[1][1]+1, $weeks[1][1]+7);
    $weeks[3] = array($weeks[2][1]+1, $weeks[2][1]+7);
    if($weeks[3][1] + 7 <= $lastDayofMonth) {
        $weeks[4] = array($weeks[3][1]+1, $weeks[3][1]+7);
        if($weeks[4][1] + 7 <= $lastDayofMonth) {
            $weeks[5] = array($weeks[4][1]+1, $weeks[4][1]+7);
            if($week[5][1] + 7 <= $lastDayofMonth) {
                $weeks[6] = array($weeks[5][1]+1, $lastDayofMonth);
            }
        } else {
            $weeks[5] = array($weeks[4][1]+1, $lastDayofMonth);
        }
    } else {
        $weeks[4] = array($weeks[3][1]+1, $lastDayofMonth);
    }

        $start = $weeks[$week][0];
        $end = $weeks[$week][1];
        $starttime = date("Y-m-$start", $firstDayofMonth);
        $endtime = date("Y-m-$end", $firstDayofMonth);
}

$sql = "SELECT sum(c.price) FROM course_order o, course c WHERE o.status = 2 AND c.no = o.course AND o.updated BETWEEN '$starttime' AND '$endtime'";
list($revenueofC) = mysql_fetch_row(mysql_query($sql));
$revenueofC = number_format($revenueofC);
$sql = "SELECT count(1) FROM course_order o, course c WHERE o.status = 2 AND c.no = o.course AND o.updated BETWEEN '$starttime' AND '$endtime'";
list($countofC) = mysql_fetch_row(mysql_query($sql));
$sql = "SELECT sum(b.price) FROM book_order o, book b WHERE o.status = 2 AND b.no = o.book AND o.updated BETWEEN '$starttime' AND '$endtime'";
list($revenueofB) = mysql_fetch_row(mysql_query($sql));
$revenueofB = number_format($revenueofB);
$sql = "SELECT count(1) FROM book_order o, book b WHERE o.status = 2 AND b.no = o.book AND o.updated BETWEEN '$starttime' AND '$endtime'";
list($countofB) = mysql_fetch_row(mysql_query($sql));


$thisYear = date('Y');
for($i = 2008; $i <= $thisYear; $i++) {
        $years[$i]= array('y' => "{$i}년");
        if($i == $year) {
            $years[$i]['s'] = 'selected="selected"';
        }
}

$tpl->define('basic',"superAnalysis.html");
$tpl->print_('basic');

require_once('admin.foot.php');
